Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Python Array → Array Intro

Python Array

Array Intro

Arrays in Python: A Deep Dive

Python doesn't have a built-in array type in the same way as languages like C or Java. Instead, the closest equivalent is the `list`. However, for numerical computations where performance is crucial, Python offers the `array` module and the `NumPy` library, providing more specialized array structures. Let's explore each:

1. Lists as Arrays (Python's built-in approach)

Python lists are versatile, dynamic collections that can hold elements of different data types. While they can be used like arrays, they aren't optimized for numerical operations like true arrays in other languages. Declaration using square brackets `[]`
Declaring Arrays using [] my_list = [1, 2, 3, 4, 5] # List of integers mixed_list = [1, 'hello', 3.14, True] # List with mixed data types print(mixed_list)

Output

[1, 'hello', 3.14, True]
Access Elements using zero-based index
Access Elements in array my_list = [1, 2, 3, 4, 5] # List of integers mixed_list = [1, 'hello', 3.14, True] first_element = my_list[0] # first_element will be 1 last_element = my_list[-1] # last_element will be 5 (negative indexing from the end) print(first_element,last_element)

Output

1 5
Slicing
Slicing aray my_list = [1, 2, 3, 4, 5] # List of integers sub_list = my_list[1:4] # sub_list will be [2, 3, 4] (elements from index 1 up to, but not including, index 4) print(sub_list)

Output

[2, 3, 4]
Mutability
Array Mutability my_list[0] = 10 # Modifies the first element
Limitations as arrays Because lists can hold mixed data types, they have some overhead. This makes them less efficient for numerical computations compared to specialized array structures. Operations like element-wise addition or multiplication across a large list will be slower than with a `NumPy` array.

2. The `array` Module (for homogeneous data)

The `array` module provides a more compact array structure that stores elements of the *same* data type. This homogeneity allows for better memory efficiency and can improve performance slightly compared to lists for numerical operations, but still falls short of `NumPy`. Declaration using a type code
Declaration using a type code import array integer_array = array.array('i', [1, 2, 3, 4, 5]) # 'i' indicates signed integer float_array = array.array('f', [1.1, 2.2, 3.3]) # 'f' indicates float print(integer_array,'\n',float_array)

Output

array('i', [1, 2, 3, 4, 5]) array('f', [1.100000023841858, 2.200000047683716, 3.299999952316284])
Type Codes Various type codes exist for different data types (e.g., 'i' for signed integer, 'f' for float, 'd' for double-precision float, 'u' for Unicode character, etc.). Refer to Python's documentation for a complete list. Operations The `array` module provides methods for appending, inserting, removing, and other basic operations, but it lacks the extensive mathematical functions of `NumPy`.

3. NumPy Arrays (for powerful numerical computation)

`NumPy` is a fundamental library for numerical computation in Python. Its `ndarray` (n-dimensional array) object is highly optimized for numerical operations, offering significantly better performance than lists or the `array` module. Declaration using functions like `numpy.array()`
Declaring numpy array import numpy as np numpy_array = np.array([1, 2, 3, 4, 5]) two_d_array = np.array([[1, 2], [3, 4]])
✦ Data Types `NumPy` arrays have a homogeneous data type, but type inference is often automatic. ✦ Broadcasting `NumPy` supports broadcasting, enabling element-wise operations between arrays of different shapes (under certain conditions). ✦ Vectorized Operations `NumPy` allows for vectorized operations, applying the same operation to all elements of an array simultaneously. This is much faster than using loops. ✦ Functions `NumPy` provides a vast collection of mathematical functions (linear algebra, statistics, Fourier transforms, etc.) that work directly on arrays.

Summary

✦ Lists General-purpose, dynamic, mutable, can hold mixed data types, not optimized for numerical computation. ✦ `array` module More memory-efficient than lists for homogeneous data, but lacks the features and performance of `NumPy`. ✦ `NumPy` arrays Highly optimized for numerical computation, supports vectorization and broadcasting, provides a wealth of mathematical functions. ✦ For most numerical applications in Python, `NumPy` arrays are the preferred choice due to their speed and functionality. Lists are suitable when you need a flexible collection that can hold elements of different types, while the `array` module might offer a slight performance advantage over lists for simple numerical tasks with homogeneous data, but generally, NumPy outperforms both.

Tutorials